package com.shycloud.mido.music.service.impl;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.shycloud.mido.music.service.MisMusicUploadService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

/**
 * @author Xiaoxing Yu
 * @date 2020/8/12
 **/
@AllArgsConstructor
@Service
public class MisMusicUploadServiceImpl implements MisMusicUploadService {

	private Auth auth;

	private QiniuUploadCallbackHandler qiniuUploadCallbackHandler;

	/**
	 * 七牛云上传文件至指定bucket【华南节点】
	 *
	 * @param data		数据
	 * @param key		文件Key
	 * @return
	 */
	@SneakyThrows
	@Override
	public void uploadFileAsync(byte[] data, String key, String bucket) throws QiniuException {
		Configuration cfg = new Configuration(Zone.zone2());
		UploadManager uploadManager = new UploadManager(cfg);
		uploadManager.asyncPut(data, key, getUploadToken(key, bucket),
				null, null,false, qiniuUploadCallbackHandler);
	}

	/**
	 * 获取覆盖上传凭证(指定bucket)
	 *
	 * @return
	 */
	@SneakyThrows
	public String getUploadToken(String key, String myBucket) {
		return this.auth.uploadToken(myBucket, key);
	}

}
